/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package routine;

import routine.utility.NoteRow;
import javax.swing.*;

/**
 *
 * @author Wizard
 */
public class SettingForm extends javax.swing.JDialog 
{

    /**
     * Creates new form SettingForm
     */
    public SettingForm() {
        initComponents();
        this.setModal(true);
        this.setLocationRelativeTo(null);
        
        startHrSpinner.setModel(new SpinnerNumberModel(0, 0, 23, 1));
        endHrSpinner.setModel(new SpinnerNumberModel(0, 0, 23, 1));
        tipHrSpinner.setModel(new SpinnerNumberModel(0, 0, 23, 1));
        sStartMinSpinner.setModel(new SpinnerNumberModel(0, 0, 59, 1));
        endMinSpinner.setModel(new SpinnerNumberModel(0, 0, 59, 1));
        tipMinSpinner.setModel(new SpinnerNumberModel(0, 0, 59, 1));
    }

    public boolean isSaved()
    {
        return saved;
    }
    
    public void setNote(NoteRow note)
    {
        this.note = note;
        nameText.setText(note.getTitle());
        int time = note.getStartTime();
        int min = time % 100;
        int hr = time / 100;
        startHrSpinner.setValue(hr);
        sStartMinSpinner.setValue(min);
        time = note.getEndTime();
        min = time % 100;
        hr = time / 100;
        endHrSpinner.setValue(hr);
        endMinSpinner.setValue(min);
        int method = note.getAlertType();
        tipMethodComboBox.setSelectedIndex(method);
        if(method != 0)
        {
            time = note.getAlertTime();
            min = time % 100;
            hr = time / 100;
        }
        else
            min = hr = 0;
        tipHrSpinner.setValue(hr);
        tipMinSpinner.setValue(min);
        extraText.setText(note.getComment());
    }
    
    @Override
    public void setVisible(boolean b)
    {
        if(b) this.saved = false; //Show
        super.setVisible(b);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        nameText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        startHrSpinner = new javax.swing.JSpinner();
        sStartMinSpinner = new javax.swing.JSpinner();
        endHrSpinner = new javax.swing.JSpinner();
        endMinSpinner = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tipMethodComboBox = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        tipHrSpinner = new javax.swing.JSpinner();
        tipMinSpinner = new javax.swing.JSpinner();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        extraText = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setTitle("设置");
        setResizable(false);

        jLabel1.setText("事件名称");

        jLabel2.setText("起始时间");

        jLabel3.setText(":");

        jLabel4.setText(":");

        jLabel5.setText("截止时间");

        tipMethodComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "无", "提前一天", "当天" }));
        tipMethodComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tipMethodComboBox_SelectChange(evt);
            }
        });

        jLabel6.setText("提醒方式");

        tipHrSpinner.setEnabled(false);

        tipMinSpinner.setEnabled(false);

        jLabel7.setText(":");

        jLabel8.setText("提醒时间");

        jLabel9.setText("备注");

        saveButton.setText("保存");
        saveButton.setPreferredSize(new java.awt.Dimension(80, 23));
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButton_Click(evt);
            }
        });

        cancelButton.setText("取消");
        cancelButton.setPreferredSize(new java.awt.Dimension(80, 23));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButton_Click(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(startHrSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(sStartMinSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(endHrSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(nameText)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(endMinSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tipMethodComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tipHrSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tipMinSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(0, 0, Short.MAX_VALUE)
                                        .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel9)
                                        .addGap(28, 28, 28)
                                        .addComponent(extraText)))
                                .addGap(2, 2, 2)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(startHrSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(sStartMinSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(endHrSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(endMinSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tipMethodComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(tipHrSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tipMinSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(extraText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButton_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButton_Click
        //Saved = false;
        this.setVisible(false);
    }//GEN-LAST:event_cancelButton_Click

    private void saveButton_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButton_Click
        String name = nameText.getText();
        if(name.equals(""))
        {
            JOptionPane.showMessageDialog(null, "事件名称不得为空！");
            return;
        }
        if(name.length() > 32)
        {
            JOptionPane.showMessageDialog(null, "事件名称不得大于32个字符！");
            return;
        }
        note.setTitle(name);
        int hr = (Integer)startHrSpinner.getValue();
        int min = (Integer)sStartMinSpinner.getValue();
        int starttime = hr * 100 + min;
        note.setStartTime(starttime);
        hr = (Integer)endHrSpinner.getValue();
        min = (Integer)endMinSpinner.getValue();
        int endtime = hr * 100 + min;
        if(endtime < starttime)
        {
            JOptionPane.showMessageDialog(null, "结束时间不得小于起始时间！");
            return;
        }
        note.setEndTime(endtime);
        int method = tipMethodComboBox.getSelectedIndex();
        note.setAlertType(method);
        int alerttime = 0;
        if(method != 0)
        {
            hr = (Integer)tipHrSpinner.getValue();
            min = (Integer)tipMinSpinner.getValue();
            alerttime = hr * 100 + min;
        }
        if(method == 2 && starttime < alerttime)
        {
            JOptionPane.showMessageDialog(null, "起始时间不得小于提醒时间！");
            return;
        }
        note.setAlertTime(alerttime);
        String comment = extraText.getText();
        if(comment.length() > 256)
        {
            JOptionPane.showMessageDialog(null, "备注不得大于256个字符！");
            return;
        }
        note.setComment(comment);
        saved = true;
        this.setVisible(false);
    }//GEN-LAST:event_saveButton_Click

    private void tipMethodComboBox_SelectChange(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tipMethodComboBox_SelectChange
        int index = tipMethodComboBox.getSelectedIndex();
        boolean enabled = (index != 0);
        tipHrSpinner.setEnabled(enabled);
        tipMinSpinner.setEnabled(enabled);
    }//GEN-LAST:event_tipMethodComboBox_SelectChange


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JSpinner endHrSpinner;
    private javax.swing.JSpinner endMinSpinner;
    private javax.swing.JTextField extraText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField nameText;
    private javax.swing.JSpinner sStartMinSpinner;
    private javax.swing.JButton saveButton;
    private javax.swing.JSpinner startHrSpinner;
    private javax.swing.JSpinner tipHrSpinner;
    private javax.swing.JComboBox tipMethodComboBox;
    private javax.swing.JSpinner tipMinSpinner;
    // End of variables declaration//GEN-END:variables

    private NoteRow note;
    private boolean saved;
}
